package com.wang.controller;


import com.wang.base.http.RestResponse;
import com.wang.base.util.TokenUtil;
import com.wang.domain.User;
import com.wang.domain.enums.UserRoleEnum;
import com.wang.model.User.*;
import com.wang.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Objects;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WangZai
 * @since 2023-03-25
 */
@CrossOrigin
@RestController
@RequestMapping("/td/api/user")
@Api(tags = "用户模块接口说明")
public class UserController {
    private final ModelMapper modelMapper;
    private final UserService userService;
    private static final Logger log = LoggerFactory.getLogger(TongueDiagnosisController.class);

    @Autowired
    public UserController(ModelMapper modelMapper, UserService userService) {
        this.modelMapper = modelMapper;
        this.userService = userService;
    }

    /**
     * 功能描述 通过token获取用户信息
     *
     * @param authentication
     * @return com.zhu.base.http.RestResponse<java.lang.Object>
     * @author JoyBoyZH
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/current")
    @ApiOperation("获取当前用户信息接口说明")
    public RestResponse<Object> current(@RequestHeader("authentication") String authentication) {
        String userName = TokenUtil.getUserInfoFromToken(authentication);
        User user = userService.getUserByUserName(userName);
        UserResponseVM model = modelMapper.map(user, UserResponseVM.class);
        return RestResponse.ok(model);
    }

    /**
     * 功能描述 注册接口
     *
     * @param model
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/register")
    @ApiOperation("注册接口说明")
    public RestResponse<Object> register(@RequestBody UserRegisterVM model) {
        User user = modelMapper.map(model, User.class);
        user.setRole(UserRoleEnum.USER);
        User user1 = userService.getUserByUserName(user.getUserName());
        if (user1 != null) {
            return RestResponse.failure("信息重复");
        } else {
            userService.saveUser(user);
            return RestResponse.ok("注册成功");
        }
    }

    /**
     * 功能描述 修改信息接口
     *
     * @param model
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/modify")
    @ApiOperation("修改信息接口说明")
    public RestResponse<Object> modify(@RequestBody UserModifyVM model, @RequestHeader("authentication") String authentication) {
        String userName = TokenUtil.getUserInfoFromToken(authentication);
        User user = userService.getUserByUserName(userName);

        User user1 = userService.getUserByPhone(model.getPhone());

        User user2 = userService.getUserByEmail(model.getEmail());

        if (user1 != null && !user1.getUserId().equals(user.getUserId())) {
            return RestResponse.failure("信息重复,修改失败");
        }
        if (user2 != null && user2.getUserId().equals(user.getUserId())) {
            return RestResponse.failure("信息重复,修改失败");
        }
        /**
         * 前端设置默认值，不能为空
         */
        user.setRealName(model.getRealName());
        user.setPhone(model.getPhone());
        user.setEmail(model.getEmail());
        user.setSex(model.getSex());
        user.setDate(model.getDate());
        userService.updateUserById(user);
        return RestResponse.ok("修改成功");
    }

    /**
     * 功能描述 找回密码-验证信息接口
     *
     * @param model
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/verify")
    @ApiOperation("找回密码-验证信息接口说明")
    public RestResponse<Object> verify(@RequestBody UserVerifyVM model) {
        User user1 = userService.getUserByUserName(model.getUserName());
        User user2 = userService.getUserByPhone(model.getPhone());
        if (user1 != null && user2 != null && user1.getUserId().equals(user2.getUserId())) {
            return RestResponse.ok("验证通过");
        } else {
            return RestResponse.failure("验证失败");
        }
    }

    /**
     * 功能描述 重置密码接口
     *
     * @param model
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/reset")
    @ApiOperation("重置密码接口")
    public RestResponse<Object> reset(@RequestBody UserResetPwVM model) {
        User user = userService.getUserByUserName(model.getUserName());
        String password = user.getPassword();
        user.setPassword(model.getRePassword());
        if (userService.updateUserById(user) && !Objects.equals(password, model.getRePassword())) {
            return RestResponse.ok("修改成功，请重新登录");
        } else {
            return RestResponse.failure("重置失败!新密码不能与最近密码相同");
        }
    }

}

